# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=('openssl' 'libopenssl' 'openssl-devel')
_ver=1.1.1g
# use a pacman compatible version scheme
pkgver=${_ver/[a-z]/.${_ver//[0-9.]/}}
pkgrel=2
pkgdesc='The Open Source toolkit for Secure Sockets Layer and Transport Layer Security'
arch=('i686' 'x86_64')
url='https://www.openssl.org'
license=('custom:BSD')
depends=('zlib')
makedepends=('gcc' 'tar' 'perl' 'zlib-devel' 'diffutils' 'nasm')
options=('!makeflags' 'staticlibs')
noextract=(${pkgname}-${_ver}.tar.gz)
source=("https://www.openssl.org/source/${pkgname}-${_ver}.tar.gz"{,.asc}
        '0001-Use-usr-ssl-as-ca-dir-instead-of-.-demoCA.patch'
        '0002-Support-MSYS2.patch'
        '0003-tls13secretstest-work-around-multiple-definition-of.patch'
        '0004-Override-engines-directory.patch')
sha256sums=('ddb04774f1e32f0c49751e21b67216ac87852ceb056b75209af2443400636d46'
            'SKIP'
            '35f5508bc3e651bb2ef1bf9f4e052f531aeaefbbdeff95b9746fff71137fff31'
            '0f6d46bb1025edd71d518e48fc640f88521b715bcfce2fd2c2a0266dfff4efd6'
            'a83dcfeec646724f081f8ff89beccee83c962758aa74d724f263138aa21b2438'
            '79765cc10799691ae47880556255287c64b8c1f26ea3907ad2e0add97bc9f1fc')
validpgpkeys=('8657ABB260F056B1E5190839D9C4D26D0E604491'
              '7953AC1FBC3DC8B3B292393ED5E9E43F7DF9EE8C')

prepare() {
  [[ -d ${srcdir}/${pkgname}-${_ver} ]] || tar -xzvf ${srcdir}/${pkgname}-${_ver}.tar.gz -C ${srcdir}
  cd ${srcdir}/${pkgname}-${_ver}

  patch -p1 -i ${srcdir}/0001-Use-usr-ssl-as-ca-dir-instead-of-.-demoCA.patch
  patch -p1 -i ${srcdir}/0002-Support-MSYS2.patch
  patch -p1 -i ${srcdir}/0003-tls13secretstest-work-around-multiple-definition-of.patch
  patch -p1 -i ${srcdir}/0004-Override-engines-directory.patch
}

build() {
  cd ${srcdir}/${pkgname}-${_ver}

  if [ "${CARCH}" == 'x86_64' ]; then
    openssltarget='Msys-x86_64'
  elif [ "${CARCH}" == 'i686' ]; then
    openssltarget='Msys'
  fi

  ./Configure \
    --prefix=/usr \
    --openssldir=/usr/ssl \
    --libdir=lib \
    shared \
    zlib \
    no-idea \
    no-rc5 \
    ${openssltarget}

  make depend
  make all
  make DESTDIR=${srcdir}/dest MANDIR=/usr/share/man MANSUFFIX=ssl install
}

check() {
  cd ${srcdir}/${pkgname}-${_ver}

  # the test would write test data into /usr/ssl, which we do not want
  patch -R -p1 -i ${srcdir}/0001-Use-usr-ssl-as-ca-dir-instead-of-.-demoCA.patch
  make test
  patch -p1 -i ${srcdir}/0001-Use-usr-ssl-as-ca-dir-instead-of-.-demoCA.patch
}

package_openssl() {
  depends=('libopenssl' 'zlib')
  optdepends=('ca-certificates' 'perl')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.exe ${pkgdir}/usr/bin/
  cp -f ${srcdir}/dest/usr/bin/c_rehash ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/ssl ${pkgdir}/usr/
  install -D -m644 ${srcdir}/${pkgname}-${_ver}/LICENSE ${pkgdir}/usr/share/licenses/${pkgname}/LICENSE
}

package_libopenssl() {
  depends=('zlib')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/

  mkdir -p ${pkgdir}/usr/lib/openssl
  cp -rf ${srcdir}/dest/usr/lib/openssl/engines-1.1 ${pkgdir}/usr/lib/openssl/
  chmod -R 755 ${pkgdir}/usr/lib/openssl/engines-1.1
}

package_openssl-devel() {
  pkgdesc="Openssl headers and libraries"
  groups=('development')
  depends=("libopenssl=${pkgver}" 'zlib-devel')

  mkdir -p ${pkgdir}/usr/lib
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib/pkgconfig ${pkgdir}/usr/lib/
  cp -f ${srcdir}/dest/usr/lib/*.a ${pkgdir}/usr/lib
}
